// var n;
// var $pPSDK_Config = require("./AL_Framework_AL_SDK_CONFIG");
// var $macros = require("./AL_Framework_Macros");
// var l = cc._decorator;
// var d = l.ccclass;
// var u = l.property;
// var h = (function (e) {
//     function t() {
//         var t = (null !== e && e.apply(this, arguments)) || this;
//         t.panel = null;
//         t.node_loading_icon = null;
//         t.label = null;
//         return t;
//     }
//     __extends(t, e);
//     t.prototype.onLoad = function () {
//         this._ani();
//     };
//     t.prototype.show = function (e) {
//         this.node.active = !0;
//         this.label.string = e;
//         return Promise.resolve();
//     };
//     t.prototype.hide = function () {
//         this.node.active = !1;
//         return Promise.resolve();
//     };
//     t.prototype._ani = function () {
//         cc.tween(this.node_loading_icon)
//             .by(3, {
//                 angle: 360
//             })
//             .repeatForever()
//             .start();
//     };
//     __decorate([u(cc.Node)], t.prototype, "panel", void 0);
//     __decorate([u(cc.Node)], t.prototype, "node_loading_icon", void 0);
//     __decorate([u(cc.Label)], t.prototype, "label", void 0);
//     return __decorate([d], t);
// })(cc.Component);
// var p = (function () {
//     function e() {
//         this.logTag = "[Loading]";
//         this._timeout_callback = null;
//         this._timeout_id = -1;
//     }
//     e.Instance = function () {
//         return this._instance || (this._instance = new e());
//     };
//     e.prototype.jiazai_yuzhiti = function () {
//         var e = this;
//         return new Promise(function (t, o) {
//             var i = "HUA/framework/common/components/loading/loadingNode";
//             Manager.cacheManager.yibuHuoquHuancun(i, cc.Prefab, $macros.Macro.BUNDLE_RESOURCES).then(function (n) {
//                 n
//                     ? (Manager.assetManager.addPersistAsset(i, n, $macros.Macro.BUNDLE_RESOURCES),
//                       t(n),
//                       e.shezhi_yuzhiti(n, h))
//                     : (Log.e(e.logTag, "Loading 预制体加载失败:" + i), o());
//             });
//         });
//     };
//     e.prototype.shezhi_yuzhiti = function (e, t) {
//         if (this._huoqu_yuzhiti_xinxi(e).getComponent(t)) {
//             this._prefab = e;
//             this._type = t;
//             var o = cc.instantiate(this._prefab);
//             cc.Canvas.instance.node.parent.addChild(o);
//             cc.game.addPersistRootNode(o);
//             this._loading = o.getComponent(this._type);
//             o.active = !1;
//         } else {
//             Log.e(this.logTag, "未挂载" + t.prototype.constructor.name + ",将无法使用，请排查");
//         }
//     };
//     e.prototype.init = function () {
//         return $pPSDK_Config.Config.USE_SYS_LOADING ? this.jiazai_yuzhiti() : Promise.resolve();
//     };
//     e.prototype.show = function (e) {
//         var t = this;
//         if (void 0 === e) {
//             e = {};
//         }
//         if (this._prefab && this._loading) {
//             this._timeout_callback = e.timeoutCb;
//             this._show_callback = e.showCb;
//             this._hide_callback = e.hideCb;
//             if (void 0 === e.mask) {
//                 e.mask = !0;
//             }
//             this._xianshiYincangShuru(e.mask);
//             if ("number" != typeof e.timeout) {
//                 e.timeout = $pPSDK_Config.Config.LOADING_TIME_OUT;
//             }
//             if (void 0 === e.msg) {
//                 e.msg = Manager.languageManager.huoqu_biaoqian_neirong("framework", "加载中...");
//             }
//             var o = this._loading.show(e.msg);
//             o instanceof Promise
//                 ? o.then(function () {
//                       if (t._show_callback) {
//                           t._show_callback();
//                       }
//                       t._kaishiDaojishi(e.timeout);
//                   })
//                 : (this._show_callback && this._show_callback(), this._kaishiDaojishi(e.timeout));
//             if ("number" != typeof e.delay) {
//                 e.delay = $pPSDK_Config.Config.LOADING_DELAY;
//             }
//             if (e.delay > 0) {
//                 this._loading.node.opacity = 0;
//                 this._loading.node.stopAllActions();
//                 cc.tween(this._loading.node)
//                     .delay(e.delay)
//                     .to(0.3, {
//                         opacity: 255
//                     })
//                     .start();
//             }
//         } else {
//             Log.e(this.logTag, "未加载系统Loading组件,msg:" + e.msg);
//         }
//     };
//     e.prototype.hide = function (e) {
//         var t = this;
//         this._prefab && this._loading
//             ? (e && (this._hide_callback = e),
//               this._hide().then(function () {
//                   if (t._hide_callback) {
//                       t._hide_callback();
//                   }
//               }))
//             : Log.e(this.logTag, "未加载系统Loading组件");
//     };
//     e.prototype._hide = function () {
//         clearTimeout(this._timeout_id);
//         this._timeout_id = -1;
//         var e = this._loading.hide();
//         return e instanceof Promise ? e : Promise.resolve();
//     };
//     e.prototype._kaishiDaojishi = function (e) {
//         var t = this;
//         if (e > 0) {
//             this._timeout_id = setTimeout(function () {
//                 if (t._timeout_callback) {
//                     t._timeout_callback();
//                 }
//                 t._hide();
//             }, 1e3 * e);
//         }
//     };
//     e.prototype._xianshiYincangShuru = function (e) {
//         if (!this._blockInput) {
//             var t = this._loading.getComponent(cc.BlockInputEvents);
//             if (t) {
//                 this._blockInput = t;
//             }
//         }
//         e
//             ? (this._blockInput || (this._blockInput = this._loading.addComponent(cc.BlockInputEvents)),
//               (this._blockInput.enabled = !0))
//             : this._blockInput && (this._blockInput.enabled = !1);
//     };
//     e.prototype._huoqu_yuzhiti_xinxi = function (e) {
//         return e instanceof cc.Prefab ? e.data : e;
//     };
//     e._instance = null;
//     return e;
// })();
// exports.default = p;


const AL_Framework_AL_SDK_CONFIG = require("./AL_Framework_AL_SDK_CONFIG");
const AL_Framework_Macros = require("./AL_Framework_Macros");
const { ccclass, property } = cc._decorator;

/** 
 * LoadingComponent类，用于展示加载中的动画
 * @class
 */
@ccclass
class LoadingComponent extends cc.Component {
  @property(cc.Node)
  panel = null;

  @property(cc.Node)
  node_loading_icon = null;

  @property(cc.Label)
  label = null;

  onLoad() {
    this._start_Animation();
  }

  /**
   * 显示Loading
   * @param {string} message - 显示的消息
   * @returns {Promise<void>}
   */
  show(message) {
    this.node.active = true;
    this.label.string = message;
    return Promise.resolve();
  }

  /**
   * 隐藏Loading
   * @returns {Promise<void>}
   */
  hide() {
    this.node.active = false;
    return Promise.resolve();
  }

  /**
   * 开始Loading动画
   * @private
   */
  _start_Animation() {
    cc.tween(this.node_loading_icon)
      .by(3, { angle: 360 })
      .repeatForever()
      .start();
  }
}

/** 
 * LoadingManager类，单例模式，用于管理Loading界面
 * @class
 */
class LoadingManager {
  constructor() {
    this.logTag = "[Loading]";
    this._timeout_callback = null;
    this._timeout_id = -1;
    this._loading = null;
    this._prefab = null;
    this._type = null;
    this._blockInput = null;
  }

  static Instance() {
    if (!this._instance) {
      this._instance = new LoadingManager();
    }
    return this._instance;
  }

  /**
   * 加载Loading预制体
   * @returns {Promise<cc.Prefab>}
   */
  jiazai_yuzhiti() {
    const loadingPath = "HUA/framework/common/components/loading/loadingNode";
    return new Promise((resolve, reject) => {
      Manager.cacheManager.yibuHuoquHuancun(loadingPath, cc.Prefab, AL_Framework_Macros.Macro.BUNDLE_RESOURCES)
        .then((prefab) => {
          if (prefab) {
            Manager.assetManager.addPersistAsset(loadingPath, prefab, AL_Framework_Macros.Macro.BUNDLE_RESOURCES);
            resolve(prefab);
            this.shezhi_yuzhiti(prefab, LoadingComponent);
          } else {
            Log.e(this.logTag, `Loading 预制体加载失败: ${loadingPath}`);
            reject();
          }
        });
    });
  }

  /**
   * 设置Loading预制体
   * @param {cc.Prefab} prefab - 预制体
   * @param {typeof LoadingComponent} componentType - 组件类型
   */
  shezhi_yuzhiti(prefab, componentType) {
    const prefabInfo = this._huoqu_yuzhiti_xinxi(prefab);
    if (prefabInfo.getComponent(componentType)) {
      this._prefab = prefab;
      this._type = componentType;
      const instance = cc.instantiate(this._prefab);
      cc.Canvas.instance.node.parent.addChild(instance);
      cc.game.addPersistRootNode(instance);
      this._loading = instance.getComponent(this._type);
      instance.active = false;
    } else {
      Log.e(this.logTag, `未挂载${componentType.prototype.constructor.name},将无法使用，请排查`);
    }
  }

  init() {
    return AL_Framework_AL_SDK_CONFIG.Config.USE_SYS_LOADING ? this.jiazai_yuzhiti() : Promise.resolve();
  }

  /**
   * 显示Loading界面
   * @param {object} [options={}] - 配置项
   */
  show(options = {}) {
    if (this._prefab && this._loading) {
      this._timeout_callback = options.timeoutCb;
      this._show_callback = options.showCb;
      this._hide_callback = options.hideCb;
      options.mask = options.mask !== undefined ? options.mask : true;
      this._xianshiYincangShuru(options.mask);
      options.timeout = typeof options.timeout === "number" ? options.timeout : AL_Framework_AL_SDK_CONFIG.Config.LOADING_TIME_OUT;
      options.msg = options.msg || Manager.languageManager.huoqu_biaoqian_neirong("framework", "加载中...");
      const showPromise = this._loading.show(options.msg);
      if (showPromise instanceof Promise) {
        showPromise.then(() => {
          this._show_callback && this._show_callback();
          this._kaishiDaojishi(options.timeout);
        });
      } else {
        this._show_callback && this._show_callback();
        this._kaishiDaojishi(options.timeout);
      }

      options.delay = typeof options.delay === "number" ? options.delay : AL_Framework_AL_SDK_CONFIG.Config.LOADING_DELAY;
      if (options.delay > 0) {
        this._loading.node.opacity = 0;
        this._loading.node.stopAllActions();
        cc.tween(this._loading.node)
          .delay(options.delay)
          .to(0.3, { opacity: 255 })
          .start();
      }
    } else {
      Log.e(this.logTag, `未加载系统Loading组件,msg: ${options.msg}`);
    }
  }

  /**
   * 隐藏Loading界面
   * @param {function} [callback] - 隐藏后的回调
   */
  hide(callback) {
    if (this._prefab && this._loading) {
      this._hide_callback = callback || this._hide_callback;
      this._hide().then(() => {
        this._hide_callback && this._hide_callback();
      });
    } else {
      Log.e(this.logTag, "未加载系统Loading组件");
    }
  }

  /**
   * 隐藏Loading动画
   * @private
   * @returns {Promise<void>}
   */
  _hide() {
    clearTimeout(this._timeout_id);
    this._timeout_id = -1;
    const hidePromise = this._loading.hide();
    return hidePromise instanceof Promise ? hidePromise : Promise.resolve();
  }

  /**
   * 开始超时计时
   * @private
   * @param {number} timeout - 超时时间
   */
  _kaishiDaojishi(timeout) {
    if (timeout > 0) {
      this._timeout_id = setTimeout(() => {
        this._timeout_callback && this._timeout_callback();
        this._hide();
      }, timeout * 1000);
    }
  }

  /**
   * 显示或隐藏屏蔽输入事件
   * @private
   * @param {boolean} mask - 是否屏蔽输入
   */
  _xianshiYincangShuru(mask) {
    if (!this._blockInput) {
      const blockInputComponent = this._loading.getComponent(cc.BlockInputEvents);
      if (blockInputComponent) {
        this._blockInput = blockInputComponent;
      }
    }
    if (mask) {
      this._blockInput = this._blockInput || this._loading.addComponent(cc.BlockInputEvents);
      this._blockInput.enabled = true;
    } else if (this._blockInput) {
      this._blockInput.enabled = false;
    }
  }

  /**
   * 获取预制体信息
   * @private
   * @param {cc.Prefab} prefab - 预制体
   * @returns {cc.PrefabInfo}
   */
  _huoqu_yuzhiti_xinxi(prefab) {
    return prefab instanceof cc.Prefab ? prefab.data : prefab;
  }
}

LoadingManager._instance = null;

exports.default = LoadingManager;
